﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;
using ZigBeeLibrary;
using ComLibrary;
using CloudModels;
using CommonLibrary;
using Newtonsoft.Json;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        List<DataPoint> list_point = new List<DataPoint>();
        ZigBee zigbe = null;
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            lbl_konqi.Text = Sensor_data(138, true);
            ComSettingModel models = new ComSettingModel();
            models.ZigbeeCom = "COM1";
            zigbe = new ZigBee(models);
            zigbe.InitSerialPort();
            zigbe.GetSet();
            Label2.Text = zigbe.lightValue;

            int i = Convert.ToInt32(Label1.Text);
            i++;
            Label1.Text = i.ToString();
            ChartArea chartArea = new ChartArea();


            Series series = new Series();
            series.ChartType = SeriesChartType.Line;
            Random r = new Random();
            series.XAxisType = AxisType.Secondary;
            Axis xaxix = new Axis();
            xaxix.IntervalAutoMode = IntervalAutoMode.FixedCount;
            xaxix.IntervalType = DateTimeIntervalType.Seconds;
            xaxix.IsLabelAutoFit = true;
            xaxix.IsStartedFromZero = true;
            //series.XValueType = ChartValueType.DateTime;
            xaxix.Minimum = 0;
            xaxix.Interval = 1;
            xaxix.Maximum = 60;

            Axis yaxis = new Axis();

            yaxis.IntervalAutoMode = IntervalAutoMode.FixedCount;
            yaxis.Interval = 2;
            yaxis.Maximum = 40;

            for (int j = 0; j < 10; j++)
            {
                //  Thread.Sleep(1000);
                DataPoint point = new DataPoint();
                point.MarkerStyle = MarkerStyle.Circle;
                double[] dd = new double[1];
                dd[0] = Convert.ToDouble(r.Next(0, 10));
                point.YValues = dd;

                // point.XValue = Convert.ToDouble(DateTime.Now.Second);
                series.Points.Add(point);
            }
            chartArea.AxisX = xaxix;
            chartArea.AxisX.IsStartedFromZero = true;
            chartArea.AxisX.LabelStyle.IsStaggered = true;
            chartArea.AxisY = yaxis;

            Chart1.Series.Add(series);
            Chart1.ChartAreas.Add(chartArea);
          
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Button1.Text.Contains("灯光一开"))
            {
                set_deice(1, true);
                Button1.Text = "灯光一关";
            }
            else {
                set_deice(1, false);
                Button1.Text = "灯光一开";
            }
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Button2.Text.Contains("灯光二开"))
            {
                set_deice(2, true);
                Button2.Text = "灯光二关";
            }
            else
            {
                set_deice(2, false);
                Button2.Text = "灯光二开";
            }
        }

        public bool set_deice(int device,bool is_open) {
            bool is_ok = false;
            HttpHelper help = new HttpHelper("http://172.16.11.63/");
            string str_result= Common.GetToken("http://172.16.11.63/", "user16", "123456", "wodiao");
            if (!string.IsNullOrWhiteSpace(str_result)) {
                help.RequestData("OnOff", "Device", "OnOff?actuatorId=" + device + "&isOpen=" + is_open, "AccessToken", str_result);
                string result= help.ResultMsg;
                if (!string.IsNullOrWhiteSpace(result)) {
                    ResponseModel model = JsonConvert.DeserializeObject<ResponseModel>(result);
                    if(model!=null){
                        is_ok = true;
                    }
                }
            }
            return is_ok;
        }

        public string Sensor_data(int Sensor_id,bool is_unit) {
            string Data = "";
            HttpHelper help = new HttpHelper("http://172.16.11.63/");
            string str_result= Common.GetToken("http://172.16.11.63/", "user16", "123456", "wodiao");
            if(!string.IsNullOrWhiteSpace(str_result)){
                help.RequestData("GetNewestSensorData", "SensorData", "GetNewestSensorData?sensorId=" + Sensor_id, "AccessToken", str_result);
                string result= help.ResultMsg;
                if(!string.IsNullOrWhiteSpace(result)){
                    ResponseModel model= JsonConvert.DeserializeObject<ResponseModel>(result);
                    if(model!=null&&model.AppendData.ToString()!=null){
                         SensorDataModel sensor_data = JsonConvert.DeserializeObject<SensorDataModel>(model.AppendData.ToString());
                        if(sensor_data!=null){
                        if(is_unit){
                            Data = sensor_data.SensorData + " " + sensor_data.SensorUnit;
                        }else{
                            Data = sensor_data.SensorData;
                        }
                        }

                    }
                }
            }
            return Data;
        
        }
    }
}